home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / hugearr.zip / HUGEDIM.C < prev    next >
C/C++ Source or Header  |  1992-04-02  |  3KB  |  119 lines

  1.  
  2. #define NOCOMM
  3. #include <windows.h>
  4.  
  5. #include "hugearr.h"
  6.  
  7. /* Get the handle of the next unused array slot. */
  8. static int CDECL
  9. VBHugeGetFreeArray(PHUGEDESC pArray)
  10.     {
  11.     int i = 0;
  12.  
  13.     /* loop until found or out of entries */
  14.     while (i < NumArrays && pArray -> handle != NULL)
  15.         {
  16.         ++pArray;
  17.         ++i;
  18.         }
  19.  
  20.     if (i == NumArrays)
  21.         /* didn't find a spot */
  22.         return HA_TOOMANYARRAYS;
  23.  
  24.     /* found one, return index to it */
  25.     return i;
  26.     }
  27.  
  28. /* Allocate or reallocate the Global memory space required for a huge array,
  29.    and setup the array description structure. */
  30. static int CDECL
  31. VBHugeAlloc(PHUGEDESC pArray, int recsize, long ubound, BOOL realloc)
  32.     {
  33.     int    perseg;  /* #elements per segment */
  34.     long   newsiz;  /* actual size of array */
  35.     HANDLE handle;  /* temp handle for alloc */
  36.  
  37.     /* #elements per segment. */
  38.     perseg = (int) (0x10000L / (long) recsize);
  39.     /* size of array in bytes is offset of beginning of last element plus length of an element. */
  40.         newsiz = HugeElementOffset(ubound, perseg, recsize) + (long) recsize;
  41.  
  42.     if (!realloc)
  43.         /* allocate a new array */
  44.         handle = GlobalAlloc(GMEM_MOVEABLE || GMEM_ZEROINIT, newsiz);
  45.     else
  46.         /* attempt to reallocate the existing array */
  47.         handle = SjsGlobalReAlloc(pArray -> handle, newsiz, GMEM_MOVEABLE || GMEM_ZEROINIT);
  48.  
  49.     if (handle == NULL)
  50.         /* out of memory */
  51.         return HA_OUTOFMEMORY;
  52.  
  53.     /* save new handle */
  54.     pArray -> handle = handle;
  55.     /* save element size */
  56.     pArray -> recsize = recsize;
  57.     /* note - number of elements is ubound + 1. */
  58.     pArray -> ubound = ubound;
  59.     pArray -> perseg = perseg;
  60.  
  61.     return HA_OK;
  62.     }
  63.  
  64. /* Dimension a huge array and return a handle to it. */
  65. /* VBM: Declare Function VBHugeDim% Lib "hugearr.dll" Alias "VBHugeDim" (ByVal recsize%, ByVal limit&) */
  66. int FAR PASCAL
  67. VBHugeDim(int recsize, long ubound)
  68.     {
  69.     int       hArray;  /* handle to array to dimension */
  70.     PHUGEDESC pArray;  /* pointer to array descriptor */
  71.     int       ret;     /* return value from HugeAlloc */
  72.  
  73.     pArray = (PHUGEDESC) LocalLock(hLocalMem);
  74.  
  75.     /* find a free array */
  76.     if ((hArray = VBHugeGetFreeArray(pArray)) == HA_TOOMANYARRAYS)
  77.         {
  78.         /* couldn't find one, return error.*/
  79.         LocalUnlock(hLocalMem);
  80.         return HA_TOOMANYARRAYS;
  81.         }
  82.  
  83.     /* allocate new array */
  84.     ret = VBHugeAlloc(pArray + hArray, recsize, ubound, FALSE);
  85.     LocalUnlock(hLocalMem);
  86.  
  87.         /* if an error occured during alloc */
  88.     if (ret < 0)
  89.         /* return the error, else */
  90.         return ret;
  91.     else
  92.         /* return the handle to the array */
  93.         /* VB users think handles start at 1 */
  94.         return hArray + 1;
  95.     }
  96.  
  97. /* Redimension a huge array and return a handle to it. */
  98. /* VBM: Declare Function VBHugeRedim% Lib "hugearr.dll" Alias "VBHugeRedim" (ByVal hArray%, ByVal limit&) */
  99. int FAR PASCAL
  100. VBHugeRedim(int hArray, long ubound)
  101.     {
  102.     PHUGEDESC pArray;  /* pointer to array desciptor */
  103.     register  ret;     /* return code of HugeAlloc */
  104.  
  105.     DecCheckHandle(hArray);
  106.  
  107.     pArray = (PHUGEDESC) LocalLock(hLocalMem) + hArray;
  108.  
  109.     if (pArray -> handle != NULL)
  110.         /* reallocate array */
  111.         ret = VBHugeAlloc(pArray, pArray -> recsize, ubound, TRUE);
  112.     else
  113.         /* array has never been allocated */
  114.         ret = HA_BADARRAY;
  115.  
  116.     LocalUnlock(hLocalMem);
  117.     return ret;
  118.     }
  119.